//
//  KZTabBarController.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/10.
//  Copyright © 2018年 zhukai. All rights reserved.
//

#import "KZTabBarController.h"

#import "KZVC1.h"
#import "KZVC2.h"
#import "KZVC3.h"
#import "KZKouYuInputTestVC.h"

#import "KZReviewExamVC.h"          // 备考中心
#import "KZMistakesCoachingVC.h"    // 错题辅导
#import "KZStudySituationVC.h"      // 学情分析
#import "KZUserCenterVC.h"          // 个人中心
@interface KZTabBarController ()

@end

@implementation KZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];


    KZBaseNaviVC *vc1 = [[KZBaseNaviVC alloc] initWithRootViewController:[KZReviewExamVC new]];
    KZBaseNaviVC *vc2 = [[KZBaseNaviVC alloc] initWithRootViewController:[KZMistakesCoachingVC new]];
    KZBaseNaviVC *vc3 = [[KZBaseNaviVC alloc] initWithRootViewController:[KZStudySituationVC new]];
    KZBaseNaviVC *vc4 = [[KZBaseNaviVC alloc] initWithRootViewController:[KZKouYuInputTestVC new]];
    KZBaseNaviVC *vc5 = [[KZBaseNaviVC alloc] initWithRootViewController:[KZUserCenterVC new]];

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12], NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
    
    [self setItem:vc1 title:@"备考中心" imageName:@"tabbar_image_1" selectedImageName:@"tabbar_image_1s"];
    [self setItem:vc2 title:@"错题辅导" imageName:@"tabbar_image_2" selectedImageName:@"tabbar_image_2s"];
    [self setItem:vc3 title:@"学情分析" imageName:@"tabbar_image_3" selectedImageName:@"tabbar_image_3s"];
    [self setItem:vc4 title:@"难题训练" imageName:@"tabbar_image_4" selectedImageName:@"tabbar_image_4s"];
    [self setItem:vc5 title:@"个人中心" imageName:@"tabbar_image_5" selectedImageName:@"tabbar_image_5s"];
    

    self.viewControllers = @[vc1, vc2, vc3, vc4, vc5];
    
    
    /* 中间按钮不是系统按钮时可以重写TabBar
     KZTabBar *tabBar = [KZTabBar new];
     [self setValue:tabBar forKeyPath:@"tabBar"];
     */
}
- (void)setItem:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

/* 重写tabBar时用到
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    self.selectedIndex = item.tag;
}
 */


@end


@implementation KZTabBar
{
    UIButton *_centerButton;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_centerButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];
    [_centerButton sizeToFit];
    [_centerButton addTarget:self action:@selector(onClickCenter) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_centerButton];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGPoint center = CGPointMake(self.width / 2, self.height / 2);
    _centerButton.center = center;
    
    // 设置原有按钮的位置
    NSInteger index = 0;
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (index == 1) {
                subview.hidden = true;
            }
            index ++;
        }
    }
    index = 0;
    for (UITabBarItem *item in self.items) {
        item.tag = index;
        index ++;
    }
}
- (void)onClickCenter {
    
    UITabBarItem *item = [[UITabBarItem alloc] init];
    item.tag = 1;
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItem:)]) {
        [self.delegate tabBar:self didSelectItem:item];
    }
}



@end


